<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="收款单号" prop="receiptOrderNo">
        <el-input
          v-model="queryParams.receiptOrderNo"
          placeholder="请输入收款单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="合同编号" prop="contractNo">
        <el-input
          v-model="queryParams.contractNo"
          placeholder="请输入合同编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>



      <el-form-item label="小区名称" prop="premiseName">
        <el-input
          v-model="queryParams.premiseName"
          placeholder="请输入小区小区名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>






      <el-form-item label="租客姓名" prop="tenantName">
        <el-input
          v-model="queryParams.tenantName"
          placeholder="请输入租客姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <!--1:未收款 2：已收款 3：已中止  4：已冻结-->
      <el-form-item label="收款状态
" prop="skStatus">
        <el-select v-model="queryParams.skStatus" placeholder="请选择收款状态
" clearable size="small">
          <el-option label="全部" value="" />
          <el-option label="未收款" value="1" />
          <el-option label="已收款" value="2" />
          <el-option label="已终止" value="3" />
          <el-option label="已冻结" value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="账单类型" prop="billType">
        <el-select v-model="queryParams.billType" placeholder="请选择账单类型" clearable size="small">
          <el-option label="全部" value="" />
          <el-option label="分期变更" value="1" />
          <el-option label="租约保理变更" value="2" />
          <el-option label="退租变更" value="3" />
          <el-option label="普通账单" value="4" />
        </el-select>
      </el-form-item>


      <el-form-item label="预定收款日期" prop="collectionDate"  label-width="150px">
        <el-date-picker clearable size="small"
          v-model="queryParams.collectionDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择预定收款日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="实际收款日期" prop="realCollectionDate" label-width="150px">
        <el-date-picker clearable size="small"
          v-model="queryParams.realCollectionDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择实际收款日期">
        </el-date-picker>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <template>
      <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
        <el-tab-pane label="房租收款计划" name="first">
          <h2><span v-if="this.summ>-1 || this.sum>-1">应收金额总计:{{this.sum}}元   实收金额总计:{{this.summ}} 元</span></h2>
          <el-table v-loading="loading" :data="planList" @selection-change="handleSelectionChange">

            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="收款单号" align="center" prop="receiptOrderNo" />
            <el-table-column label="合同编号" align="center" prop="contractNo" />
            <el-table-column label="小区名" align="center" prop="premiseName" />
            <el-table-column label="承租房间" align="center" prop="roomName" />
            <el-table-column label="租客姓名" align="center" prop="tenantName" />
            <el-table-column label="收款状态" align="center" prop="skStatus" >
              <template slot-scope="scope">
              <span v-if="scope.row.skStatus==1">
                未收款
              </span>
                <span v-if="scope.row.skStatus==2">
                已收款
              </span>
                <span v-if="scope.row.skStatus==3">
                已终止
              </span>
                <span v-if="scope.row.skStatus==4">
                已冻结
              </span>
              </template>

            </el-table-column>
            <el-table-column label="账单类型" align="center" prop="billType" >
              <template slot-scope="scope">
              <span v-if="scope.row.billType==1">
                分期变更
              </span>
                <span v-if="scope.row.billType==2">
                租约保理变更
              </span>
                <span v-if="scope.row.billType==3">
                退租变更
              </span>
                <span v-if="scope.row.billType==4">
                普通账单
              </span>
              </template>
            </el-table-column>

            <el-table-column label="支付金额" align="center" prop="paymentAmount" />


            <el-table-column label="预定收款日期" align="center" prop="collectionDate" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.collectionDate, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="实际收款日期" align="center" prop="realCollectionDate" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.realCollectionDate, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="线下支付方式" align="center" prop="payMethod" >
              <template slot-scope="scope">
              <span v-if="scope.row.payMethod==1">
               快捷同
              </span>
                <span v-if="scope.row.payMethod==2">
                支付宝
              </span>
                <span v-if="scope.row.payMethod==3">
                微信
              </span>
                <span v-if="scope.row.payMethod==4">
                现金
              </span>
                <span v-if="scope.row.payMethod==5">
                银行卡转账
              </span>
                <span v-if="scope.row.payMethod==6">
                POS刷卡
              </span>
                <span v-if="scope.row.payMethod==7">
                其他
              </span>
              </template>
            </el-table-column>

            <el-table-column label="操作人" align="center" prop="operationUser" />
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-tab-pane>
        <el-tab-pane label="租务收款计划" name="second">
          <el-table v-loading="loading" :data="planList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="收款单号" align="center" prop="receiptOrderNo" />
            <el-table-column label="合同编号" align="center" prop="contractNo" />
            <el-table-column label="小区名" align="center" prop="premiseName" />
            <el-table-column label="承租房间" align="center" prop="roomName" />
            <el-table-column label="租客姓名" align="center" prop="tenantName" />
            <el-table-column label="收款状态" align="center" prop="skStatus" >
              <template slot-scope="scope">
              <span v-if="scope.row.skStatus==1">
                未收款
              </span>
                <span v-if="scope.row.skStatus==2">
                已收款
              </span>
                <span v-if="scope.row.skStatus==3">
                已终止
              </span>
                <span v-if="scope.row.skStatus==4">
                已冻结
              </span>
              </template>
            </el-table-column>
            <el-table-column label="账单类型" align="center" prop="billType" >
              <template slot-scope="scope">
              <span v-if="scope.row.billType==1">
                分期变更
              </span>
                <span v-if="scope.row.billType==2">
                租约保理变更
              </span>
                <span v-if="scope.row.billType==3">
                退租变更
              </span>
                <span v-if="scope.row.billType==4">
                普通账单
              </span>
              </template>
            </el-table-column>
            <el-table-column label="支付金额" align="center" prop="paymentAmount" />


            <el-table-column label="预定收款日期" align="center" prop="collectionDate" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.collectionDate, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="实际收款日期" align="center" prop="realCollectionDate" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.realCollectionDate, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="线下支付方式" align="center" prop="payMethod" >
              <template slot-scope="scope">
              <span v-if="scope.row.payMethod==1">
               快捷同
              </span>
                <span v-if="scope.row.payMethod==2">
                支付宝
              </span>
                <span v-if="scope.row.payMethod==3">
                微信
              </span>
                <span v-if="scope.row.payMethod==4">
                现金
              </span>
                <span v-if="scope.row.payMethod==5">
                银行卡转账
              </span>
                <span v-if="scope.row.payMethod==6">
                POS刷卡
              </span>
                <span v-if="scope.row.payMethod==7">
                其他
              </span>
              </template>
            </el-table-column>

            <el-table-column label="操作人" align="center" prop="operationUser" />
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-tab-pane>

      </el-tabs>
    </template>


    <!-- 添加或修改收款计划对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="收款单号" prop="receiptOrderNo">
          <el-input v-model="form.receiptOrderNo" placeholder="请输入收款单号" />
        </el-form-item>
        <el-form-item label="商户ID" prop="businessId">
          <el-input v-model="form.businessId" placeholder="请输入商户ID" />
        </el-form-item>
        <el-form-item label="合同ID" prop="contractId">
          <el-input v-model="form.contractId" placeholder="请输入合同ID" />
        </el-form-item>
        <el-form-item label="合同编号" prop="contractNo">
          <el-input v-model="form.contractNo" placeholder="请输入合同编号" />
        </el-form-item>
        <el-form-item label="合同类型," prop="contractType">
          <el-select v-model="form.contractType" placeholder="请选择合同类型,">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="解约协议ID" prop="tTerminationId">
          <el-input v-model="form.tTerminationId" placeholder="请输入解约协议ID" />
        </el-form-item>
        <el-form-item label="楼盘ID" prop="premiseId">
          <el-input v-model="form.premiseId" placeholder="请输入楼盘ID" />
        </el-form-item>
        <el-form-item label="小区(使用楼盘名)" prop="premiseName">
          <el-input v-model="form.premiseName" placeholder="请输入小区(使用楼盘名)" />
        </el-form-item>
        <el-form-item label="房源ID" prop="roomId">
          <el-input v-model="form.roomId" placeholder="请输入房源ID" />
        </el-form-item>
        <el-form-item label="房源编号" prop="roomCode">
          <el-input v-model="form.roomCode" placeholder="请输入房源编号" />
        </el-form-item>
        <el-form-item label="承租房间(使用房源名称)" prop="roomName">
          <el-input v-model="form.roomName" placeholder="请输入承租房间(使用房源名称)" />
        </el-form-item>
        <el-form-item label="业主ID" prop="ownerId">
          <el-input v-model="form.ownerId" placeholder="请输入业主ID" />
        </el-form-item>
        <el-form-item label="业主姓名" prop="ownerName">
          <el-input v-model="form.ownerName" placeholder="请输入业主姓名" />
        </el-form-item>
        <el-form-item label="租客ID" prop="tenantId">
          <el-input v-model="form.tenantId" placeholder="请输入租客ID" />
        </el-form-item>
        <el-form-item label="租客姓名(面向业主收款时，为空)" prop="tenantName">
          <el-input v-model="form.tenantName" placeholder="请输入租客姓名(面向业主收款时，为空)" />
        </el-form-item>
        <el-form-item label="收款类型
,1:房租收款 2：租务收款 3：解约结算收款 4：续约结算收款" prop="receiptType">
          <el-select v-model="form.receiptType" placeholder="请选择收款类型
,1:房租收款 2：租务收款 3：解约结算收款 4：续约结算收款">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="收款状态
1:未收款 2：已收款 3：已中止  4：已冻结">
          <el-radio-group v-model="form.skStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="账单类型" prop="billType">
          <el-select v-model="form.billType" placeholder="请选择账单类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="款项" prop="itemType">
          <el-select v-model="form.itemType" placeholder="请选择款项">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="其它款项,款项为其它的时候，输入" prop="otherItemName">
          <el-input v-model="form.otherItemName" placeholder="请输入其它款项,款项为其它的时候，输入" />
        </el-form-item>
        <el-form-item label="支付金额" prop="paymentAmount">
          <el-input v-model="form.paymentAmount" placeholder="请输入支付金额" />
        </el-form-item>
        <el-form-item label="实收金额" prop="realPaymentAmount">
          <el-input v-model="form.realPaymentAmount" placeholder="请输入实收金额" />
        </el-form-item>
        <el-form-item label="选择账期" prop="withReceiptPlanId">
          <el-input v-model="form.withReceiptPlanId" placeholder="请输入选择账期" />
        </el-form-item>
        <el-form-item label="账期开始日" prop="paymentDaysBegin">
          <el-date-picker clearable size="small"
            v-model="form.paymentDaysBegin"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择账期开始日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="账期截止日" prop="paymentDaysEnd">
          <el-date-picker clearable size="small"
            v-model="form.paymentDaysEnd"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择账期截止日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预定收款日期" prop="collectionDate">
          <el-date-picker clearable size="small"
            v-model="form.collectionDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择预定收款日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实际收款日期" prop="realCollectionDate">
          <el-date-picker clearable size="small"
            v-model="form.realCollectionDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择实际收款日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="第几期" prop="receiptNumber">
          <el-input v-model="form.receiptNumber" placeholder="请输入第几期" />
        </el-form-item>
        <el-form-item label="线下支付方式" prop="payMethod">
          <el-input v-model="form.payMethod" placeholder="请输入线下支付方式" />
        </el-form-item>
        <el-form-item label="其它支付方式
,支付方式为其它的时候，输入" prop="otherPayMethod">
          <el-input v-model="form.otherPayMethod" placeholder="请输入其它支付方式
,支付方式为其它的时候，输入" />
        </el-form-item>
        <el-form-item label="支付流水号" prop="payFlowNo">
          <el-input v-model="form.payFlowNo" placeholder="请输入支付流水号" />
        </el-form-item>
        <el-form-item label="账期房租金额" prop="rentalPrice">
          <el-input v-model="form.rentalPrice" placeholder="请输入账期房租金额" />
        </el-form-item>
        <el-form-item label="税费" prop="tax">
          <el-input v-model="form.tax" placeholder="请输入税费" />
        </el-form-item>
        <el-form-item label="操作人" prop="operationUser">
          <el-input v-model="form.operationUser" placeholder="请输入操作人" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="预留字段1" prop="reserve1">
          <el-input v-model="form.reserve1" placeholder="请输入预留字段1" />
        </el-form-item>
        <el-form-item label="预留字段2" prop="reserve2">
          <el-input v-model="form.reserve2" placeholder="请输入预留字段2" />
        </el-form-item>
        <el-form-item label="预留字段3" prop="reserve3">
          <el-input v-model="form.reserve3" placeholder="请输入预留字段3" />
        </el-form-item>
        <el-form-item label="预留字段4" prop="reserve4">
          <el-input v-model="form.reserve4" placeholder="请输入预留字段4" />
        </el-form-item>
        <el-form-item label="预留字段5" prop="reserve5">
          <el-input v-model="form.reserve5" placeholder="请输入预留字段5" />
        </el-form-item>
        <el-form-item label="删除标识,0:未删除，1：删除" prop="delFlg">
          <el-input v-model="form.delFlg" placeholder="请输入删除标识,0:未删除，1：删除" />
        </el-form-item>
        <el-form-item label="创建用户ID" prop="createUserid">
          <el-input v-model="form.createUserid" placeholder="请输入创建用户ID" />
        </el-form-item>
        <el-form-item label="更新用户ID" prop="updateUserid">
          <el-input v-model="form.updateUserid" placeholder="请输入更新用户ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPlan, getPlan, delPlan, addPlan, updatePlan } from "@/api/financial/plan";

export default {
  name: "Plan",
  components: {
  },
  data() {
    return {
      summ:0,
      sum:0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 收款计划表格数据
      planList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        receiptOrderNo: null,
        businessId: null,
        contractId: null,
        contractNo: null,
        contractType: null,
        tTerminationId: null,
        premiseId: null,
        premiseName: null,
        roomId: null,
        roomCode: null,
        roomName: null,
        ownerId: null,
        ownerName: null,
        tenantId: null,
        tenantName: null,
        receiptType: 1,
        skStatus: null,
        billType: null,
        itemType: null,
        otherItemName: null,
        paymentAmount: null,
        realPaymentAmount: null,
        withReceiptPlanId: null,
        paymentDaysBegin: null,
        paymentDaysEnd: null,
        collectionDate: null,
        realCollectionDate: null,
        receiptNumber: null,
        payMethod: null,
        otherPayMethod: null,
        payFlowNo: null,
        rentalPrice: null,
        tax: null,
        operationUser: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        updateUserid: null,
      },
      activeName: 'first',
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        receiptOrderNo: [
          { required: true, message: "收款单号不能为空", trigger: "blur" }
        ],
        businessId: [
          { required: true, message: "商户ID不能为空", trigger: "blur" }
        ],
        contractId: [
          { required: true, message: "合同ID不能为空", trigger: "blur" }
        ],
        contractNo: [
          { required: true, message: "合同编号不能为空", trigger: "blur" }
        ],
        contractType: [
          { required: true, message: "合同类型", trigger: "change" }
        ],
        premiseId: [
          { required: true, message: "楼盘ID不能为空", trigger: "blur" }
        ],
        premiseName: [
          { required: true, message: "小区(使用楼盘名)不能为空", trigger: "blur" }
        ],
        delFlg: [
          { required: true, message: "删除标识", trigger: "blur" }
        ],
        createUserid: [
          { required: true, message: "创建用户ID不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateUserid: [
          { required: true, message: "更新用户ID不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.sumList()
    this.getList();
  },
  methods: {
    /** 查询收款计划列表 */
    getList() {
      this.loading = true;
      listPlan(this.queryParams).then(response => {
        this.planList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    sumList(){
      this.queryParams.pageSize=10000;
      this.loading = true;
      listPlan(this.queryParams).then(response => {
        console.log(response)
        response.rows.forEach(resp=>{
        this.summ+=resp.realPaymentAmount;
          this.sum+=resp.paymentAmount;
        })
        console.log(this.sum);
        this.loading = false;
      });
      this.queryParams.pageSize=10;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        receiptOrderNo: null,
        businessId: null,
        contractId: null,
        contractNo: null,
        contractType: null,
        tTerminationId: null,
        premiseId: null,
        premiseName: null,
        roomId: null,
        roomCode: null,
        roomName: null,
        ownerId: null,
        ownerName: null,
        tenantId: null,
        tenantName: null,
        receiptType: null,
        skStatus: 0,
        billType: null,
        itemType: null,
        otherItemName: null,
        paymentAmount: null,
        realPaymentAmount: null,
        withReceiptPlanId: null,
        paymentDaysBegin: null,
        paymentDaysEnd: null,
        collectionDate: null,
        realCollectionDate: null,
        receiptNumber: null,
        payMethod: null,
        otherPayMethod: null,
        payFlowNo: null,
        rentalPrice: null,
        tax: null,
        operationUser: null,
        remark: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        createTime: null,
        updateUserid: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.summ=0;
      this.sum=0;
      this.queryParams.pageNum = 1;
      this.getList();
      this.sumList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加收款计划";
    },
    handleClick(tab, event) {
      console.log(tab.index)
      if (tab.index==0) {
        this.queryParams.receiptType=1;
        this.getList()
        this.sumList();
        console.log(this.sum);
      }
      if (tab.index==1) {
        this.queryParams.receiptType=2;
        this.getList()
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPlan(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改收款计划";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePlan(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPlan(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除收款计划编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPlan(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/plan/export', {
        ...this.queryParams
      }, `system_plan.xlsx`)
    }
  }
};
</script>
